import esriRequest from "@arcgis/core/request.js";
import BaseTileLayer from "@arcgis/core/layers/BaseTileLayer.js";
import WebTileLayer from '@arcgis/core/layers/WebTileLayer';
import { CreateWMTSLayer, CreateTDTLayer, CreateWebLayer, getTianDiTuTileInfo } from './utils/wmtsLayer.js';

export const GoogleLayerClass = initGoogleLayerClass()

function initGoogleLayerClass() {
    const gl = BaseTileLayer.createSubclass({
        properties: {
            urlTemplate: null
        },
        getTileUrl: function (level, row, col) {
            return  this.urlTemplate.replace("{z}", level).replace("{x}",col).replace("{y}", row);
        },
        fetchTile: function (level, row, col) {
            var url = this.getTileUrl(level, row, col);

            return esriRequest(url, {
                responseType: "image",
                allowImageDataAccess: true
            })
                .then(function (response) {
                    var image = response.data;
                    var width = this.tileInfo.size[0];
                    var height = this.tileInfo.size[0];
                    var canvas = document.createElement("canvas");
                    var context = canvas.getContext("2d");
                    canvas.width = width;
                    canvas.height = height;
                    context.drawImage(image, 0, 0, width, height);
                    return canvas;
                }.bind(this));
        }
    });
    return gl
}

 export const googleImgLayer =  new GoogleLayerClass({
    urlTemplate: "https://gac-geo.googlecnapps.cn/maps/vt?lyrs=y&gl=CN&x={x}&y={y}&z={z}",
    id: 'basemap-yx',
    title: "影像图",
    visible: true,  
    layerName: 'img',
  });

  export const googleVectorLayer = new GoogleLayerClass({
    urlTemplate: "https://gac-geo.googlecnapps.cn/maps/vt?lyrs=m&gl=CN&x={x}&y={y}&z={z}",
    title: "矢量图",
    id: 'basemap-sl',
    visible: false,
    layerName: 'vec',
  });


 const tiandituKey = '4cc66d27a3ef65764e7a58096e89aaf2';

 export const landImgLayer = new WebTileLayer({
  id: 'basemap-land',
  urlTemplate: 'https://geo.cqtancheng.com/gwc/service/wmts?layer=layerGroups&style=&tilematrixset=WebMercatorQuad&Service=WMTS&Request=GetTile&Version=1.0.0&Format=image/png&TileMatrix={level}&TileRow={row}&TileCol={col}'
});

export const tdtVecLayer = CreateTDTLayer({
  url: 'http://t0.tianditu.gov.cn/vec_w/wmts',
  layerName: 'vec',
  version: '1.0.0',
  style: 'default',
  title: '矢量图',
  tilematrixSet: 'w',
  id: 'basemap-sl',
  visible: false,
  tiandituKey: tiandituKey,
});
export const tdtImgLayer = CreateTDTLayer({
  url: 'http://t0.tianditu.gov.cn/img_w/wmts',
  layerName: 'img',
  version: '1.0.0',
  style: 'default',
  title: '影像图',
  tilematrixSet: 'w',
  id: 'basemap-yx',
  visible: true,
  tiandituKey: tiandituKey,
});

export const tdtLabelLayer = CreateTDTLayer({
  url: 'http://t0.tianditu.gov.cn/cia_w/wmts',
  layerName: 'cia',
  version: '1.0.0',
  style: 'default',
  title: '注记',
  tilematrixSet: 'w',
  id: 'basemap-zj',
  visible: true,
  tiandituKey: tiandituKey,
});